using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BHXH.Core.Domain;
using BHXH.Core.IService;
using FX.Data;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;

namespace BHXH.Core.ServiceImp
{
    public class thongtincongtyService : BaseService<thongtincongty, int>, IthongtincongtyService
    {
        static ISession mySession;
        public thongtincongtyService(string sessionFactoryConfigPath)
            : base(sessionFactoryConfigPath)
        { }

        

        public IList<thongtincongty> layTatCaDonVi()
        {
            var a = GetAll();
            return a;
        }

        public IList<taikhoan> GetDonVi_TaiKhoan()
        {

            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                IList<taikhoan> query = mySession.CreateQuery("FROM taikhoan as tk inner join fetch tk.thongtincongty as t").List<taikhoan>();
                return query;
            }
        }

        public thongtincongty GetDonViByID(string MaSoThue)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                IList<thongtincongty> query = mySession.CreateQuery("FROM thongtincongty as t where t.MaSoThue like :m")
                    .SetParameter("m", "%" + MaSoThue + "%")
                    .List<thongtincongty>();
                thongtincongty ttct = new thongtincongty();
                foreach (thongtincongty item in query)
                {
                    ttct = item;
                }
                return ttct;
            }
        }

        public bool UpdateDonVi(thongtincongty ttct)
        {   
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                mySession.Update(ttct);
                CommitChanges();
                mySession.Flush();
                return true;
            }
           
        }

        public int UpdateTen_EmailDonVi(thongtincongty ttct)
        {

            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                int a = mySession.CreateQuery("Update thongtincongty set TenCongTy =:tencongty,Email =:email where ID =:id")
                    .SetParameter("tencongty",ttct.TenCongTy)
                    .SetParameter("email",ttct.Email)
                    .SetParameter("id",ttct.ID)
                    .ExecuteUpdate();
                CommitChanges();
                mySession.Clear();
                return a;
            }
        }

        public bool AddDonVi(thongtincongty ttct)
        {

            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                var a = CreateNew(ttct);
                CommitChanges();
                return true;
            }
        }
    }

}